package com.markyao.recos.regist;

import com.markyao.annotation.RegistKey;
import com.markyao.service.redis.UtilsKeys;
import com.markyao.timer.TimerWorker;
import com.markyao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class RegistWorker{
    @Autowired
    @Qualifier("connectSet")
    Map<String, Set<String>> connectSet;

    @Autowired
    @Qualifier("providersMap")
    Map<String,Map<String,String>>providersMap;

    @Autowired
    @Qualifier("refrenceRegistrySet")
    Map<String,Set<String>>refrenceRegistrySet;

    @Autowired
    @Qualifier("serverRegistryMap")
    Map<String,Map<String,String>>serverRegistryMap;

    @Autowired
    @Qualifier("serviceRegistryMap")
    Map<String,Map<String,String>>serviceRegistryMap;

    @Autowired
    @Qualifier("urlMap")
    Map<String,String>urlMap;

    @Autowired
    @Qualifier("heartBeatMap")
    Map<String,Long>heartBeatMap;
    @Autowired
    @Qualifier("heartBeatMap0")
    Map<String,Long>heartBeatMap0;

    @Autowired
    @Qualifier("errorUrlMap")
    Map<String,String>errorUrlMap;

    @Autowired
    @Qualifier("timerWorker")
    TimerWorker timerWorker;

    public void registAppName(@RegistKey String appName, String remoteHost) {
        Map<String, String> map = serverRegistryMap.getOrDefault(appName, new HashMap<>());
        map.put(UtilsKeys.APPNAME,appName);
        map.put(UtilsKeys.REMOTEHOST,remoteHost);
        serverRegistryMap.put(appName,map);
    }

    public void registUrl(@RegistKey String appName,@RegistKey  String url) {
        Map<String, String> map = serverRegistryMap.getOrDefault(appName, new HashMap<>());
        map.put(UtilsKeys.URL,url);
        urlMap.put(url,appName);
        serverRegistryMap.put(appName,map);
    }

    public void registWeight(@RegistKey String appName, Integer weight) {
        Map<String, String> map = serverRegistryMap.getOrDefault(appName, new HashMap<>());
        map.put(UtilsKeys.WEIGHT,weight+"");
        serverRegistryMap.put(appName,map);

    }

    public void registServer00(@RegistKey String appName,@RegistKey String interfaceNAME, String clazz) {
        Map<String, String> map = serviceRegistryMap.getOrDefault(appName, new HashMap<>());
        map.put(interfaceNAME,clazz);
        serviceRegistryMap.put(appName,map);
        Map<String, String> serverMap = serverRegistryMap.get(appName);
        String weight = serverMap.get(UtilsKeys.WEIGHT);
        String url = serverMap.get(UtilsKeys.URL);
        registServer01(interfaceNAME,url, weight);
    }

    public void registServer01(@RegistKey String interfaceNAME,@RegistKey String url, String weight) {
        Map<String, String> mp2 = providersMap.getOrDefault(interfaceNAME, new HashMap<>());
        mp2.put(url, weight);
        providersMap.put(interfaceNAME,mp2);
    }

    public void registRefrence(@RegistKey String appName,@RegistKey String refrenceName) {
        Set<String> set = refrenceRegistrySet.getOrDefault(appName, new HashSet<>());
        set.add(refrenceName);
        refrenceRegistrySet.put(appName,set);
    }

    public void registConnect(String appName, String targetUrl) {
        String targetAppName = urlMap.get(targetUrl);
        registConnect0(appName, targetAppName);
    }

    public void registConnect0(String appName,@RegistKey String targetAppName) {
        Set<String> set = connectSet.getOrDefault(appName, new HashSet<>());
        set.add(targetAppName);
        connectSet.put(appName, set);
    }

    public void registHeartBeat(String appName) {
        if (heartBeatMap.containsKey(appName)){
            heartBeatMap0.put(appName,heartBeatMap.get(appName));
        }
        heartBeatMap.put(appName,timerWorker.getSec());
    }

}
